package com.study.controller;

import com.study.model.Quote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 利用resttemplate请求restful source
 * 请求restful service provider,except返回的结果如下,
 * {
 * type: "success",
 * value: {
 * id: 10,
 * quote: "Really loving Spring Boot, makes stand alone Spring apps easy."
 * }
 * }
 * 下面这个controller若不能被springBoot纳入容器中,bean完全发挥不了作用,你就看不到上面返回的json结果.
 */
//@RestController
@Component
public class FetchRestController {
    private Logger logger = LoggerFactory.getLogger(FetchRestController.class);

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder.build();
    }

    /**
     * SpringBoot中CommandLineRunner的作用
     * > 平常开发中有可能需要实现在项目启动后执行的功能，SpringBoot提供的一种简单的实现方案就是添加一个model并实现CommandLineRunner接口，实现功能的代码放在实现的run方法中
     * args???
     */
    //@Bean
    //public CommandLineRunner commandLineRunner(RestTemplate restTemplate) {
    //    CommandLineRunner commandLineRunner = args -> {
    //        Quote quoteResult = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
    //        logger.info(quoteResult.toString());
    //    };
    //    return commandLineRunner;
    //}
}
